package com.visikard.ui.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.item.ItemAudienceKard;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class AudienceKardAdapter extends BaseAdapter {

	Context context;
	ArrayList<ItemAudienceKard> data;
	LayoutInflater inflater;

	DateFormat formatter;
	DateFormat dateOnlyFormatter;
	DisplayMetrics metrics;
	DateFormat timeFormat;
	Handler mHandler;

	public AudienceKardAdapter(Context ct, ArrayList<ItemAudienceKard> dt, Handler mHandler) {
		context = ct;
		data = dt;
		this.context = ct;
		this.data = dt;
		this.mHandler = mHandler;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR);
		timeFormat = new SimpleDateFormat(Constant.TIME_FORMAT_STR_JUST_DATE, Locale.US);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.audience_list_item, null);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();
			
			viewHolder.mNameKard = (TextView) convertView.findViewById(R.id.text_name_kard);
			viewHolder.mTimeZone = (TextView) convertView.findViewById(R.id.text_redeemed_date_value);
			viewHolder.mRedeemedAt = (TextView) convertView.findViewById(R.id.text_redeemed_at_value);
			viewHolder.imgKard = (ImageView) convertView.findViewById(R.id.image_kard_audience);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ItemAudienceKard item = (ItemAudienceKard) getItem(position);
		if (item != null) {

			int mWidth = metrics.widthPixels / 4;
			int mHeight = mWidth * 3 / 2;

			RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) viewHolder.imgKard
					.getLayoutParams();
			lyKard.width = mWidth;
			lyKard.height = mHeight;

			viewHolder.mNameKard.setText(item.getKardName());
//			ImageLoader.getInstance(context).loadImageCorner(viewHolder.imgKard, item.getImageSmall(),
//						Device.getDeviceWidth() / 4, BitmapUtils.ROUND_ALL, 4, 3, R.drawable.kard_shadow_default_vertical, 0, 0);
			UILImageUtils.loadImage(viewHolder.imgKard, //image view
					item.getImageSmall(), //url
					1, //size of view
					BitmapUtils.ROUND_ALL, //round all 
					4, // round size
					3, //shadow
					R.drawable.kard_shadow_default_vertical, //shadow resource 
					0, 0); 

			viewHolder.mTimeZone.setText(item.getRedeemDate());
			viewHolder.mRedeemedAt.setText(item.getRedeemAt());
		}
		return convertView;
	}

	private class ViewHolder {
		TextView mNameKard,  mRedeemedAt, mTimeZone;
		ImageView imgKard;
	}

}
